import React , { useEffect, useState, useContext, useReducer} from 'react';

let initCount = 0;
const P = {
    init(initCount){
        return {count:initCount}
    },
    reducer(state,action){
        switch(action.type){
            case 'add':
                return {count:state.count + 1}
            case 'remove':
                return {count:state.count - 1}
            case 'reset':
                return P.init(action.payload);
            default:
                throw new Error();
        }
    }
}
const View = ()=>{


    const [state,dispatch] = useReducer(P.reducer,initCount,P.init);
    return(
        <div>
            <h3>count:{state.count}</h3>
            <input type="button" value="reset" onClick={()=>{dispatch({type:'reset',payload:initCount})}}/><br/>
            <input type="button" value="add" onClick={()=>{dispatch({type:'add'})}}/><br/>
            <input type="button" value="remove" onClick={()=>{dispatch({type:'remove'})}}/><br/>
        </div>
    )
}
export default View;
